package com.org.gameloft.GLIDER.common;

import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Aug 1, 2007
 * Time: 1:40:37 PM
 * Revision: 0
 */
public class GLIDEROptions
{
    public static final String PROPERTIES_FILE="GLIDER.properties";

    public static final String WINDOW_X = "window.pos.x";
    public static final String WINDOW_Y = "window.pos.y";
    public static final String WINDOW_W = "window.pos.width";
    public static final String WINDOW_H = "window.pos.height";
    public static final String WINDOW_STATE = "window.state";

    public static final String TOOLBAR_ITEM_COUNT = "toolbar.items.count";
    public static final String TOOLBAR_ITEM_I = "toolbar.items.item";
    public static final String TOOLBAR_WINDOW = "windows.toolbarEdit";

    private static SortedProperties properties;

    public static void load()
    {
        if (properties==null)
            properties = new SortedProperties();

        try
        {
            FileInputStream fis = new FileInputStream(PROPERTIES_FILE);
            properties.load(fis);
        }
        catch (Exception e)
        {
            GLLogger.log(e);
        }
    }

    public static void save()
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(PROPERTIES_FILE);
            properties.store(fos, "GLIDER properties file. Please do not modify directly.");
        }
        catch (Exception e)
        {
            GLLogger.log(e);
        }
    }

    public static void setProperty(String key, String val)
    {
        properties.setProperty(key, val);
    }

    public static void setProperty(String key, int val)
    {
        properties.setProperty(key, ""+val);
    }

    public static String getProperty(String key)
    {
        return properties.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue)
    {
        return properties.getProperty(key, defaultValue);
    }

    public static int getProperty(String key, int defaultValue)
    {
        String s = properties.getProperty(key);
        if (s == null)
            return defaultValue;

        try
        {
            return Integer.parseInt(s);
        }
        catch (Exception e)
        {
            return defaultValue;
        }
    }
}
